﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ComTools.DesignPattern.ProcessingPipeline
{
    public class Step1 : AProcessingStep<HttpRequestMessage, string, string>
    {
        /// <summary>
        /// 校验参数中是否带有code
        /// </summary>
        /// <param name="parameter">HttpRequestMessage</param>
        public override void ProcessCurrentStep(HttpRequestMessage parameter)
        {

            var parameters = new List<KeyValuePair<string, string>>();

            //从请求地址获得参数
            parameters = parameter.GetQueryNameValuePairs().ToList();
            // 如果没有code表示未授权，请求微信接口拉起授权登录页面
            if (parameters.Where(kv => kv.Key == "code").Count() == 0)
            {
                // 设置授权后跳转的页面为当前页面
                string reditect = HttpUtility.UrlEncode(parameter.RequestUri.AbsoluteUri);

                // 获取服务号的appid
                string sAppId = "";

                // 设置请求微信授权页面的url
                string url = "";

                ReturnResultAction(url);
            }
            else
            {
                ProcessNextStep(parameter);
            }
        }

        /// <summary>
        /// 下一步：获取用户信息
        /// </summary>
        /// <param name="parameter">HttpRequestMessage</param>
        public override void ProcessNextStep(HttpRequestMessage parameter)
        {
            var next = new Step2();
            next.ReturnResultAction = ReturnResultAction;
            next.ProcessCurrentStep(parameter);
        }
    }
}
